﻿Public Class Pictures

    Dim imageLoaded As Boolean = False


    Dim saveFileDialog1 As New SaveFileDialog
    Dim fileCnt As New Integer
    Dim baseFileName As String
    Dim baseFolder As String

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        PictureBox1.AllowDrop = True
    End Sub


    Private Sub PictureBox1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles PictureBox1.DragEnter
        Try
            If (e.Data.GetDataPresent(DataFormats.FileDrop)) Then
                e.Effect = DragDropEffects.Copy
                PictureBox1.Image = Image.FromFile(CType(e.Data.GetData(DataFormats.FileDrop), Array).GetValue(0).ToString)
                resizeBase()

                imageLoaded = True
                Label3.Visible = False
            End If
        Catch ex As Exception
            Label1.Text = ex.Message
        End Try
    End Sub

    Private Sub PasteToolStripButton_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteToolStripButton.Click
        If (Clipboard.GetDataObject().GetDataPresent("Bitmap")) Then
            PictureBox1.Image = CType(Clipboard.GetDataObject().GetData("Bitmap"), Image)

            resizeBase()
            imageLoaded = True
            Label3.Visible = False
        End If
    End Sub

    Private Sub SaveToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveToolStripButton.Click
        'Dim saveFileDialog1 As New SaveFileDialog
        saveFileDialog1.Title = "Save as JPEG"
        saveFileDialog1.ShowDialog()

        If saveFileDialog1.FileName <> "" Then
            Dim fs As System.IO.FileStream = CType(saveFileDialog1.OpenFile(), System.IO.FileStream)

            PictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
            fileCnt = 0
        End If
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click


        If saveFileDialog1.FileName = "" Then
            Dim fbd As New FolderBrowserDialog
            fbd.SelectedPath = My.Settings.local_workPath
            fbd.ShowDialog()

            saveFileDialog1.InitialDirectory = fbd.SelectedPath
            saveFileDialog1.FileName = saveFileDialog1.InitialDirectory & "\"
            baseFolder = saveFileDialog1.FileName
        End If

        If Not ToolStripTextBox1.Text.Equals(baseFileName) Then 'if changed filename then reset for new cycle
            fileCnt = 0
            saveFileDialog1.FileName = baseFolder
        End If

        If saveFileDialog1.FileName <> "" And ToolStripTextBox1.Text <> "" Then
            Try
                If saveFileDialog1.FileName.Contains("(") And saveFileDialog1.FileName.Contains(").jpg") And ToolStripTextBox1.Text.Equals(baseFileName) Then
                    'do this to save the next hires filenaem (2); (3) etc not thumb anymore
                    fileCnt += 1
                    Dim tmpFileName As String = saveFileDialog1.FileName
                    tmpFileName = tmpFileName.Substring(0, tmpFileName.LastIndexOf(CChar("(")))
                    tmpFileName = tmpFileName & "(" & CStr(fileCnt) & ").jpg"

                    saveFileDialog1.FileName = tmpFileName
                    Dim fs As System.IO.FileStream = CType(saveFileDialog1.OpenFile(), System.IO.FileStream)
                    PictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
                    fs.Close()
                    Label1.Text = tmpFileName.ToString & " [Saved OK]"

                Else
                    saveFileDialog1.FileName = saveFileDialog1.FileName.Substring(0, saveFileDialog1.FileName.LastIndexOf(CChar("\"))) & "\" & ToolStripTextBox1.Text & "-thumb.jpg"
                    Dim fs2 As System.IO.FileStream = CType(saveFileDialog1.OpenFile(), System.IO.FileStream)
                    PictureBox2.Image.Save(fs2, System.Drawing.Imaging.ImageFormat.Jpeg)
                    fs2.Close()

                    'save large picture and keep last filename in memory
                    saveFileDialog1.FileName = saveFileDialog1.FileName.Substring(0, saveFileDialog1.FileName.LastIndexOf(CChar("\"))) & "\" & ToolStripTextBox1.Text & "-hi(1).jpg"
                    Dim fs As System.IO.FileStream = CType(saveFileDialog1.OpenFile(), System.IO.FileStream)
                    PictureBox1.Image.Save(fs, System.Drawing.Imaging.ImageFormat.Jpeg)
                    fs.Close()

                    baseFileName = ToolStripTextBox1.Text
                    fileCnt += 1
                    Label1.Text = "Autoincrement Save Mode - Thumbnail and base(1) Saved OK"
                End If

            Catch ex As Exception
                Label1.Text = e.ToString
                'MessageBox.Show(e.)
            Finally


            End Try
        Else
            Label1.Text = "Error - No Filename and/or No Thumbnail"
        End If


    End Sub

    'Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    ' Get the scale factor.
    '    Dim scale_factor As Single = Single.Parse(txtScale.Text)

    '    ' Get the source bitmap.
    '    'Dim bm_source As New Bitmap(picSource.Image)
    '    Dim bm_source As New Bitmap(PictureBox1.Image)

    '    ' Make a bitmap for the result.
    '    Dim bm_dest As New Bitmap(CInt(bm_source.Width * scale_factor), CInt(bm_source.Height * scale_factor))

    '    ' Make a Graphics object for the result Bitmap.
    '    Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

    '    ' Copy the source image into the destination bitmap.
    '    gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width + 1, bm_dest.Height + 1)

    '    ' Display the result.
    '    PictureBox1.Image = bm_dest
    'End Sub

    'cropping code----------------------------
    Dim cropX As Integer
    Dim cropY As Integer
    Dim cropWidth As Integer
    Dim cropHeight As Integer
    Dim cropPen As Pen
    Dim img As Image

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        img = PictureBox1.Image
        cropX = e.X
        cropY = e.Y
        cropPen = New Pen(Color.YellowGreen, 1)
        cropPen.DashStyle = Drawing2D.DashStyle.Solid
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If MouseButtons = Windows.Forms.MouseButtons.Left Then
            'cropWidth = cropX + (cropX - e.X)
            'cropHeight = cropY + (cropY - e.Y)
            cropWidth = e.X - cropX
            cropHeight = e.Y - cropY
            Label1.Text = "X Start:" & CStr(cropX) & " e.X:" & CStr(e.X) & _
            "  Y Start:" & CStr(cropY) & " e.Y:" & CStr(e.Y) _
            & "  Size W:" & CStr(cropWidth) & " Size H:" & CStr(cropHeight)

            PictureBox1.Refresh()
            PictureBox1.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)

        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        Try
            If (cropWidth And cropHeight) > 0 And imageLoaded Then
                PictureBox1.CreateGraphics.DrawRectangle(cropPen, cropX, cropY, cropWidth, cropHeight)

                Dim rect As New Rectangle(0, 0, cropWidth, cropHeight)
                Dim src_rect As New Rectangle(cropX, cropY, cropWidth, cropHeight)
                ' Make a bitmap for the result.
                Dim bm_dest As New Bitmap(cropWidth, cropHeight)
                ' Make a Graphics object for the result Bitmap.
                Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
                ' Copy the source image into the destination bitmap.
                gr_dest.DrawImage(PictureBox1.Image, rect, src_rect, GraphicsUnit.Pixel)

                ' Display the result.
                PictureBox2.Image = bm_dest

                resizeToThumb()
            End If

        Catch ex As Exception
            MessageBox.Show("Error: " & ex.ToString)
        End Try
      

    End Sub

    Private Sub resizeBase()
        Try
            ' Get the source bitmap.
            'Dim bm_source As New Bitmap(picSource.Image)
            Dim bm_source As New Bitmap(PictureBox1.Image)
            Dim origWidth As Integer = PictureBox1.Width
            Dim origHeight As Integer = PictureBox1.Height

            Dim bm_dest As Bitmap
            ' Make a bitmap for the result.
            If PictureBox1.Height > PictureBox1.Width Then
                bm_dest = New Bitmap(PictureBox1.Height, PictureBox1.Height)
            ElseIf PictureBox1.Width > PictureBox1.Height Then
                bm_dest = New Bitmap(PictureBox1.Width, PictureBox1.Width)
            End If

            ' Make a Graphics object for the result Bitmap.
            Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

            ' Copy the source image into the destination bitmap.
            gr_dest.Clear(Color.White)
            gr_dest.DrawImage(bm_source, 0, 0, origWidth, origHeight)

            ' Display the result.
            PictureBox1.Image = bm_dest

        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub


    Private Sub resizeToThumb()
        ' Get the source bitmap.
        'Dim bm_source As New Bitmap(picSource.Image)
        Dim bm_source As New Bitmap(PictureBox2.Image)

        ' Make a bitmap for the result.
        Dim bm_dest As New Bitmap(135, 100)

        ' Make a Graphics object for the result Bitmap.
        Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)

        ' Copy the source image into the destination bitmap.
        gr_dest.DrawImage(bm_source, 0, 0, bm_dest.Width, bm_dest.Height)

        ' Display the result.
        PictureBox2.Image = bm_dest
    End Sub

    'rem ---------------------------------------------------------



    Private Sub ToolStripTextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles ToolStripTextBox1.KeyUp
        Dim clr As New Bitmap(135, 100)
        PictureBox2.Image = clr
        Label1.Text = "Normal Save Mode - Counters Reset"
        'Label3.Visible = True
    End Sub

    Private Sub OpenToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripButton.Click

    End Sub
End Class
